
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/21/2012 20:30:19
-- Generated from EDMX file: D:\Projects\Labs\4th semester\CSharp\Smolyakov.INISP.4sem.Labs\DAL\MovieRental.EfRepositoryLibrary\MovieRentalEntities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [MovieRentalEntities];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_EditionMovie]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Editions] DROP CONSTRAINT [FK_EditionMovie];
GO
IF OBJECT_ID(N'[dbo].[FK_EditionMediaType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Editions] DROP CONSTRAINT [FK_EditionMediaType];
GO
IF OBJECT_ID(N'[dbo].[FK_LoanCartCustomer]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[LoanCarts] DROP CONSTRAINT [FK_LoanCartCustomer];
GO
IF OBJECT_ID(N'[dbo].[FK_LoanCartLoan]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Loans] DROP CONSTRAINT [FK_LoanCartLoan];
GO
IF OBJECT_ID(N'[dbo].[FK_LoanEdition]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Loans] DROP CONSTRAINT [FK_LoanEdition];
GO
IF OBJECT_ID(N'[dbo].[FK_TemporaryDiscountEdition]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Editions] DROP CONSTRAINT [FK_TemporaryDiscountEdition];
GO
IF OBJECT_ID(N'[dbo].[FK_TemporaryDiscount_inherits_Discount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discounts_TemporaryDiscount] DROP CONSTRAINT [FK_TemporaryDiscount_inherits_Discount];
GO
IF OBJECT_ID(N'[dbo].[FK_CartDiscount_inherits_Discount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discounts_CartDiscount] DROP CONSTRAINT [FK_CartDiscount_inherits_Discount];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonalDiscount_inherits_Discount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Discounts_PersonalDiscount] DROP CONSTRAINT [FK_PersonalDiscount_inherits_Discount];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Customers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Customers];
GO
IF OBJECT_ID(N'[dbo].[Movies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Movies];
GO
IF OBJECT_ID(N'[dbo].[MediaTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MediaTypes];
GO
IF OBJECT_ID(N'[dbo].[Discounts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Discounts];
GO
IF OBJECT_ID(N'[dbo].[Editions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Editions];
GO
IF OBJECT_ID(N'[dbo].[LoanCarts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LoanCarts];
GO
IF OBJECT_ID(N'[dbo].[Loans]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Loans];
GO
IF OBJECT_ID(N'[dbo].[Discounts_TemporaryDiscount]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Discounts_TemporaryDiscount];
GO
IF OBJECT_ID(N'[dbo].[Discounts_CartDiscount]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Discounts_CartDiscount];
GO
IF OBJECT_ID(N'[dbo].[Discounts_PersonalDiscount]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Discounts_PersonalDiscount];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Customers'
CREATE TABLE [dbo].[Customers] (
    [Id] uniqueidentifier  NOT NULL,
    [FirstName] nvarchar(30)  NOT NULL,
    [LastName] nvarchar(30)  NOT NULL,
    [Email] nvarchar(50)  NOT NULL,
    [PhoneNo] nvarchar(30)  NOT NULL
);
GO

-- Creating table 'Movies'
CREATE TABLE [dbo].[Movies] (
    [Id] uniqueidentifier  NOT NULL,
    [Title] nvarchar(50)  NOT NULL,
    [WebPage] nvarchar(50)  NOT NULL,
    [ImdbRating] float  NOT NULL
);
GO

-- Creating table 'MediaTypes'
CREATE TABLE [dbo].[MediaTypes] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(30)  NOT NULL,
    [DefaultPricePerDay] float  NOT NULL
);
GO

-- Creating table 'Discounts'
CREATE TABLE [dbo].[Discounts] (
    [Id] uniqueidentifier  NOT NULL,
    [Value] float  NOT NULL
);
GO

-- Creating table 'Editions'
CREATE TABLE [dbo].[Editions] (
    [Id] uniqueidentifier  NOT NULL,
    [MovieId] uniqueidentifier  NOT NULL,
    [MediaTypeId] uniqueidentifier  NOT NULL,
    [Publisher] nvarchar(30)  NOT NULL,
    [PricePerDay] float  NOT NULL,
    [Quantity] int  NOT NULL,
    [TemporaryDiscountId] uniqueidentifier  NULL
);
GO

-- Creating table 'LoanCarts'
CREATE TABLE [dbo].[LoanCarts] (
    [Id] uniqueidentifier  NOT NULL,
    [CustomerId] uniqueidentifier  NULL,
    [CartDiscountValue] float  NOT NULL,
    [LoanDate] datetime  NOT NULL
);
GO

-- Creating table 'Loans'
CREATE TABLE [dbo].[Loans] (
    [LoanCartId] uniqueidentifier  NOT NULL,
    [PricePerDay] float  NOT NULL,
    [Id] uniqueidentifier  NOT NULL,
    [ReturnDate] datetime  NULL,
    [EditionId] uniqueidentifier  NULL
);
GO

-- Creating table 'Discounts_TemporaryDiscount'
CREATE TABLE [dbo].[Discounts_TemporaryDiscount] (
    [ValidUntil] datetime  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Discounts_CartDiscount'
CREATE TABLE [dbo].[Discounts_CartDiscount] (
    [QuantityCondition] int  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Discounts_PersonalDiscount'
CREATE TABLE [dbo].[Discounts_PersonalDiscount] (
    [SpentMoneyCondition] float  NOT NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Customers'
ALTER TABLE [dbo].[Customers]
ADD CONSTRAINT [PK_Customers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Movies'
ALTER TABLE [dbo].[Movies]
ADD CONSTRAINT [PK_Movies]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'MediaTypes'
ALTER TABLE [dbo].[MediaTypes]
ADD CONSTRAINT [PK_MediaTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Discounts'
ALTER TABLE [dbo].[Discounts]
ADD CONSTRAINT [PK_Discounts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Editions'
ALTER TABLE [dbo].[Editions]
ADD CONSTRAINT [PK_Editions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'LoanCarts'
ALTER TABLE [dbo].[LoanCarts]
ADD CONSTRAINT [PK_LoanCarts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Loans'
ALTER TABLE [dbo].[Loans]
ADD CONSTRAINT [PK_Loans]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Discounts_TemporaryDiscount'
ALTER TABLE [dbo].[Discounts_TemporaryDiscount]
ADD CONSTRAINT [PK_Discounts_TemporaryDiscount]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Discounts_CartDiscount'
ALTER TABLE [dbo].[Discounts_CartDiscount]
ADD CONSTRAINT [PK_Discounts_CartDiscount]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Discounts_PersonalDiscount'
ALTER TABLE [dbo].[Discounts_PersonalDiscount]
ADD CONSTRAINT [PK_Discounts_PersonalDiscount]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [MovieId] in table 'Editions'
ALTER TABLE [dbo].[Editions]
ADD CONSTRAINT [FK_EditionMovie]
    FOREIGN KEY ([MovieId])
    REFERENCES [dbo].[Movies]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EditionMovie'
CREATE INDEX [IX_FK_EditionMovie]
ON [dbo].[Editions]
    ([MovieId]);
GO

-- Creating foreign key on [MediaTypeId] in table 'Editions'
ALTER TABLE [dbo].[Editions]
ADD CONSTRAINT [FK_EditionMediaType]
    FOREIGN KEY ([MediaTypeId])
    REFERENCES [dbo].[MediaTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EditionMediaType'
CREATE INDEX [IX_FK_EditionMediaType]
ON [dbo].[Editions]
    ([MediaTypeId]);
GO

-- Creating foreign key on [CustomerId] in table 'LoanCarts'
ALTER TABLE [dbo].[LoanCarts]
ADD CONSTRAINT [FK_LoanCartCustomer]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[Customers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LoanCartCustomer'
CREATE INDEX [IX_FK_LoanCartCustomer]
ON [dbo].[LoanCarts]
    ([CustomerId]);
GO

-- Creating foreign key on [LoanCartId] in table 'Loans'
ALTER TABLE [dbo].[Loans]
ADD CONSTRAINT [FK_LoanCartLoan]
    FOREIGN KEY ([LoanCartId])
    REFERENCES [dbo].[LoanCarts]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LoanCartLoan'
CREATE INDEX [IX_FK_LoanCartLoan]
ON [dbo].[Loans]
    ([LoanCartId]);
GO

-- Creating foreign key on [EditionId] in table 'Loans'
ALTER TABLE [dbo].[Loans]
ADD CONSTRAINT [FK_LoanEdition]
    FOREIGN KEY ([EditionId])
    REFERENCES [dbo].[Editions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LoanEdition'
CREATE INDEX [IX_FK_LoanEdition]
ON [dbo].[Loans]
    ([EditionId]);
GO

-- Creating foreign key on [TemporaryDiscountId] in table 'Editions'
ALTER TABLE [dbo].[Editions]
ADD CONSTRAINT [FK_TemporaryDiscountEdition]
    FOREIGN KEY ([TemporaryDiscountId])
    REFERENCES [dbo].[Discounts_TemporaryDiscount]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TemporaryDiscountEdition'
CREATE INDEX [IX_FK_TemporaryDiscountEdition]
ON [dbo].[Editions]
    ([TemporaryDiscountId]);
GO

-- Creating foreign key on [Id] in table 'Discounts_TemporaryDiscount'
ALTER TABLE [dbo].[Discounts_TemporaryDiscount]
ADD CONSTRAINT [FK_TemporaryDiscount_inherits_Discount]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Discounts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Discounts_CartDiscount'
ALTER TABLE [dbo].[Discounts_CartDiscount]
ADD CONSTRAINT [FK_CartDiscount_inherits_Discount]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Discounts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Discounts_PersonalDiscount'
ALTER TABLE [dbo].[Discounts_PersonalDiscount]
ADD CONSTRAINT [FK_PersonalDiscount_inherits_Discount]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Discounts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------